package com.hitcoder.chat.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.hitcoder.chat.R
import com.hitcoder.chat.model.AppBean
import java.util.*

class ChatFunAdapter(context: Context?, data: ArrayList<AppBean>?) : BaseAdapter() {

    private val inflater: LayoutInflater = LayoutInflater.from(context)

    private var appBeans = ArrayList<AppBean>()

    init {
        if (data != null) {
            appBeans = data
        }
    }

    override fun getCount(): Int {
        return appBeans.size
    }

    override fun getItem(position: Int): Any {
        return appBeans[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var convertView1 = convertView
        val viewHolder = if (convertView1 == null) {
            ViewHolder().apply {
                convertView1 = inflater.inflate(R.layout.item_add_function, parent, false)
                ivIcon = convertView1?.findViewById(R.id.iv_icon)
                tvName = convertView1?.findViewById(R.id.tv_name)
                convertView1?.tag = this
            }
        } else {
            convertView1?.tag as ViewHolder
        }
        appBeans[position].let {
            viewHolder.ivIcon!!.setText(it.iconText)
            viewHolder.tvName!!.text = it.funcName
            viewHolder.ivIcon!!.isEnabled = it.isEnable
        }
        return convertView1!!
    }

    internal class ViewHolder {
        var ivIcon: TextView? = null
        var tvName: TextView? = null
    }


}